﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LockTime
{
    /// <summary>
    /// 形状按钮
    /// 可以自定义按钮的形状
    /// </summary>
    public class ShapeButton :Button
    {
        Color clr1, clr2;
        //颜色
        private Color color1 = Color.DodgerBlue;
        private Color color2 = Color.MidnightBlue;
        //悬浮色
        private Color hovercolor1 = Color.Turquoise;
        private Color hovercolor2 = Color.DarkSlateGray;
        //透明色
        private int color1Transparent = 250;
        private int color2Transparent = 250;
        //点击色
        private Color clickcolor1 = Color.Yellow;
        private Color clickcolor2 = Color.Red;
        //角度
        private int angle = 90;
        //文字坐标
        private int textX = 0;
        private int textY = 0;
        private String text = "";
        //按钮边框颜色
        public Color buttonborder = Color.FromArgb(220, 220, 220);
        public Boolean showButtonText = true;
        public int borderWidth = 2;
        public Color borderColor = Color.Transparent;
        //按钮形状
        public enum ButtonsShapes
        {
            //可以根据自己的情况新增
            RoundRect,
            Circle
        }

        ButtonsShapes buttonShape;
        //按钮形状
        public ButtonsShapes ButtonShape
        {
            get { return buttonShape; }
            set
            {
                buttonShape = value; Invalidate();
            }
        }
        //按钮文本
        public String ButtonText
        {
            get { return text; }
            set { text = value; Invalidate(); }
        }
        //边框宽度
        public int BorderWidth
        {
            get { return borderWidth; }
            set { borderWidth = value; Invalidate(); }
        }

        /// <summary>
        /// 设置边框颜色
        /// </summary>
        /// <param name="bdrColor"></param>
        void SetBorderColor(Color bdrColor)
        {
            int red = bdrColor.R - 40;
            int green = bdrColor.G - 40;
            int blue = bdrColor.B - 40;
            if (red < 0)
                red = 0;
            if (green < 0)
                green = 0;
            if (blue < 0)
                blue = 0;
            //将色彩进行一定处理
            buttonborder = Color.FromArgb(red, green, blue);
        }

        //边框颜色
        public Color BorderColor
        {
            get { return borderColor; }
            set
            {
                borderColor = value;
                if (borderColor == Color.Transparent)
                {
                    buttonborder = Color.FromArgb(220, 220, 220);
                }
                else
                {
                    SetBorderColor(borderColor);
                }

            }
        }
        //开始颜色
        public Color StartColor
        {
            get { return color1; }
            set { color1 = value; Invalidate(); }
        }
        //结束颜色
        public Color EndColor
        {
            get { return color2; }
            set { color2 = value; Invalidate(); }
        }
        //悬浮色1
        public Color MouseHoverColor1
        {
            get { return hovercolor1; }
            set { hovercolor1 = value; Invalidate(); }
        }
        //悬浮色2
        public Color MouseHoverColor2
        {
            get { return hovercolor2; }
            set { hovercolor2 = value; Invalidate(); }
        }
        //点击色1
        public Color MouseClickColor1
        {
            get { return clickcolor1; }
            set { clickcolor1 = value; Invalidate(); }
        }
        //点击色2
        public Color MouseClickColor2
        {
            get { return clickcolor2; }
            set { clickcolor2 = value; Invalidate(); }
        }
        //透明色1
        public int Transparent1
        {
            get { return color1Transparent; }
            set
            {
                color1Transparent = value;
                if (color1Transparent > 255)
                {
                    color1Transparent = 255;
                    Invalidate();
                }
                else
                    Invalidate();
            }
        }
        //透明色2
        public int Transparent2
        {
            get { return color2Transparent; }
            set
            {
                color2Transparent = value;
                if (color2Transparent > 255)
                {
                    color2Transparent = 255;
                    Invalidate();
                }
                else
                    Invalidate();
            }
        }
        //角度
        public int GradientAngle
        {
            get { return angle; }
            set { angle = value; Invalidate(); }
        }
        //文本坐标
        public int TextLocation_X
        {
            get { return textX; }
            set { textX = value; Invalidate(); }
        }
        public int TextLocation_Y
        {
            get { return textY; }
            set { textY = value; Invalidate(); }
        }
        //显示文本
        public Boolean ShowButtontext
        {
            get { return showButtonText; }
            set { showButtonText = value; Invalidate(); }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public ShapeButton()
        {
            //设置一些基本属性
            this.Size = new Size(100, 40);
            this.BackColor = Color.Transparent;
            this.FlatStyle = FlatStyle.Flat;
            this.FlatAppearance.BorderSize = 0;
            this.FlatAppearance.MouseOverBackColor = Color.Transparent;
            this.FlatAppearance.MouseDownBackColor = Color.Transparent;
            this.Size = new Size(180, 36);
            this.ForeColor = Color.Yellow;
            this.Font = new Font("Microsoft YaHei UI", 18.25F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(0)));

        }
        /// <summary>
        /// 重写鼠标进入事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            //设置悬浮色
            clr1 = color1;
            clr2 = color2;
            color1 = hovercolor1;
            color2 = hovercolor2;
        }
        /// <summary>
        /// 重写鼠标离开事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            //恢复颜色
            color1 = clr1;
            color2 = clr2;
            //设置边框色
            SetBorderColor(borderColor);
        }
        /// <summary>
        /// 重写鼠标按下事件
        /// </summary>
        /// <param name="mevent"></param>
        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            base.OnMouseDown(mevent);
            //设置点击色
            color1 = clickcolor1;
            color2 = clickcolor2;
            //设置边框色
            buttonborder = borderColor;
            SetBorderColor(borderColor);
            this.Invalidate();
        }
        /// <summary>
        /// 重写鼠标抬起事件
        /// </summary>
        /// <param name="mevent"></param>
        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            base.OnMouseUp(mevent);
            OnMouseLeave(mevent);
            //恢复颜色
            color1 = clr1;
            color2 = clr2;
            //设置边框色
            SetBorderColor(borderColor);
            this.Invalidate();
        }
        /// <summary>
        /// 重写失去焦点事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            color1 = clr1;
            color2 = clr2;
            this.Invalidate();
        }
        /// <summary>
        /// 重写改变大小事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            textX = (int)ClientRectangle.Width / 4;
            textY = (int)ClientRectangle.Height / 4;
        }
        /// <summary>
        /// 绘制椭圆形按钮
        /// </summary>
        /// <param name="g"></param>
        void DrawCircularButton(Graphics g)
        {
            Color c1 = Color.FromArgb(color1Transparent, color1);
            Color c2 = Color.FromArgb(color2Transparent, color2);

            //绘制一个椭圆形
            Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, angle);
            g.FillEllipse(b, 5, 5, this.Width - 10, this.Height - 10);
            //绘制边框
            for (int i = 0; i < borderWidth; i++)
            {
                g.DrawEllipse(new Pen(new SolidBrush(buttonborder)), 5 + i, 5, this.Width - 10, this.Height - 10);
            }

            //显示按钮文字
            if (showButtonText)
            {
                Point p = new Point(textX, textY);
                SolidBrush frcolor = new SolidBrush(this.ForeColor);
                g.DrawString(text, this.Font, frcolor, p);
            }

            b.Dispose();
        }

        /// <summary>
        /// 绘制圆角正方形按钮
        /// </summary>
        /// <param name="g"></param>
        void DrawRoundRectangularButton(Graphics g)
        {
            Color c1 = Color.FromArgb(color1Transparent, color1);
            Color c2 = Color.FromArgb(color2Transparent, color2);

            //绘制圆角正方形
            Brush b = new LinearGradientBrush(ClientRectangle, c1, c2, angle);

            Region region = new Region(new Rectangle(5, 5, this.Width, this.Height));

            GraphicsPath grp = new GraphicsPath();
            grp.AddArc(5, 5, 40, 40, 180, 90);
            grp.AddLine(25, 5, this.Width - 25, 5);
            grp.AddArc(this.Width - 45, 5, 40, 40, 270, 90);
            grp.AddLine(this.Width - 5, 25, this.Width - 5, this.Height - 25);
            grp.AddArc(this.Width - 45, this.Height - 45, 40, 40, 0, 90);
            grp.AddLine(25, this.Height - 5, this.Width - 25, this.Height - 5);
            grp.AddArc(5, this.Height - 45, 40, 40, 90, 90);
            grp.AddLine(5, 25, 5, this.Height - 25);

            region.Intersect(grp);

            g.FillRegion(b, region);
            //绘制边框
            for (int i = 0; i < borderWidth; i++)
            {
                g.DrawArc(new Pen(buttonborder), 5 + i, 5 + i, 40, 40, 180, 90);
                g.DrawLine(new Pen(buttonborder), 25, 5 + i, this.Width - 25, 5 + i);
                g.DrawArc(new Pen(buttonborder), this.Width - 45 - i, 5 + i, 40, 40, 270, 90);
                g.DrawLine(new Pen(buttonborder), 5 + i, 25, 5 + i, this.Height - 25);


                g.DrawLine(new Pen(buttonborder), this.Width - 5 - i, 25, this.Width - 5 - i, this.Height - 25);
                g.DrawArc(new Pen(buttonborder), this.Width - 45 - i, this.Height - 45 - i, 40, 40, 0, 90);
                g.DrawLine(new Pen(buttonborder), 25, this.Height - 5 - i, this.Width - 25, this.Height - 5 - i);
                g.DrawArc(new Pen(buttonborder), 5 + i, this.Height - 45 - i, 40, 40, 90, 90);

            }

            //显示按钮文字
            if (showButtonText)
            {
                Point p = new Point(textX, textY);
                SolidBrush frcolor = new SolidBrush(this.ForeColor);
                g.DrawString(text, this.Font, frcolor, p);
            }

            b.Dispose();
        }

        /// <summary>
        /// 重写绘制事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //选择绘制圆形还是圆角按钮
            switch (buttonShape)
            {
                case ButtonsShapes.Circle:
                    this.DrawCircularButton(e.Graphics);
                    break;

                case ButtonsShapes.RoundRect:
                    this.DrawRoundRectangularButton(e.Graphics);
                    break;
            }
        }
    }
}
